using System;

using OR.Reuse.Persistence;
using System.Collections.Generic;
using OR.Lazarus.Business;
using System.Configuration;
using System.IO;

namespace OR.Lazarus.Config
{
	/// <summary>
	/// Summary description for LazarusConfig.
	/// </summary>
	public class LazarusConfig
	{
		public   const string   KEY_CONFIG		= "ConfigPath";
		public   const string   KEY_SOURCE		= "SourcePath";
		public   const string   KEY_TRANSFORM	= "TransformPath";
		public	 const	string	KEY_OUTPUT		= "OutputPath";

		private static string				mOutputPath;
		private static string				mConfigPath;
		private static string				mTransformPath;
		private static string				mSourcePath;
		private static string				mConfigListFile;
		private static SettingsManager	    mSettings;
		private static LogViewerManager<ILogViewer>	mLogViewerManager;

		public static LogViewerManager<ILogViewer> LogViewerManager
		{
			get { return mLogViewerManager; }
			set { mLogViewerManager = value; }
		}
		
		public static SettingsManager Settings
		{
			get
			{
				return ( mSettings );
			}
			set
			{
				mSettings = value;
			}
		}

		public static string OutputPath
		{
			get
			{
				return ( mOutputPath );
			}
			set
			{
				mOutputPath = value;
			}
		}

		public static string ConfigPath
		{
			get
			{
				return ( mConfigPath );
			}
			set
			{
				mConfigPath = value;
			}
		}

		public static string TransformPath
		{
			get
			{
				return ( mTransformPath );
			}
			set
			{
				mTransformPath = value;
			}
		}

		public static string SourcePath
		{
			get
			{
				return ( mSourcePath );
			}
			set
			{
				mSourcePath = value;
			}
		}

		public static string ConfigListFile
		{
			get
			{
				return ( mConfigListFile );
			}
			set
			{
				mConfigListFile = value;
			}
		}

		public static void Validate()
		{
			if ( String.IsNullOrEmpty( ConfigPath ) )
			{
				mSettings.LogErrorMissingKey( KEY_CONFIG );
			}

			if ( String.IsNullOrEmpty( TransformPath ) )
			{
				mSettings.LogErrorMissingKey( KEY_TRANSFORM );
			}

			if ( String.IsNullOrEmpty( SourcePath ) )
			{
				mSettings.LogErrorMissingKey( KEY_OUTPUT );
			}

			if ( String.IsNullOrEmpty( mOutputPath ) )
			{
				OutputPath = SourcePath + "output\\";
			}
		}

		public static void Initialize()
		{
			mSettings = new SettingsManager();
			mLogViewerManager = new LogViewerManager<ILogViewer>();
		}

		public static void Load()
		{
			ConfigPath		= GetDefaultRelativePath( KEY_CONFIG, "Config" );
			TransformPath	= GetDefaultRelativePath( KEY_TRANSFORM, "Transforms" );
			SourcePath		= GetDefaultRelativePath( KEY_SOURCE, "Source" );
			OutputPath		= GetDefaultRelativePath( KEY_OUTPUT, "Output" );
        }

        private static string GetDefaultRelativePath(string settingKey, string targetPath)
        {
            string currentPath = mSettings.GetValue(settingKey);
            string rtnVal = currentPath;

            if (String.IsNullOrEmpty(currentPath))
            {
                rtnVal = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, targetPath);

                mSettings.SetValue(settingKey, rtnVal);
            }
            else if (!Path.IsPathRooted(currentPath))
            {
                rtnVal = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, currentPath);
            }

            return Path.GetFullPath(rtnVal);
        }

		public static void Save()
		{
			mSettings.SetValue( KEY_CONFIG, mConfigPath );
			mSettings.SetValue( KEY_TRANSFORM, mTransformPath );
			mSettings.SetValue( KEY_SOURCE, mSourcePath );
			mSettings.SetValue( KEY_OUTPUT, mOutputPath );

            mSettings.Save();
		}

		private LazarusConfig()
		{

		}
	}
}